<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/9
 * Time: 15:06
 */

namespace app\admin\model;


use app\admin\helper\DBhelper;
use app\admin\helper\LangHelper;
use app\admin\helper\SearchHelper;
use app\admin\popo\LayData;
use think\Config;
use think\Request;
use think\Session;
use think\Validate;

class EmailM extends BaseM
{
    protected $table = 'tb_email';
    protected $module = '邮件配置';

    /*
         * rule
         * 验证data
         * @params $data
         * 新增和修改时验证
         * */
    public function validate_model()
    {
        $rule = [
            'from' => 'require|email',
            'formname' => 'require|chsDash',
            'username' => 'require|email',
            'password' => 'require',
            'host' => 'require',
            'port' => 'require|number',
        ];
        $msg = [
            'from.require' => '__mail.validata.from.require__',
            'from.email' => '__mail.validata.from.email__',
            'username.require' => '__mail.validata.username.require__',
            'username.email' => '__mail.validata.username.email__',
            'port.require' => '__mail.validata.port.require__',
            'port.number' => '__mail.validata.port.number__',
            'password.require' => '__mail.validata.password.require__',
            'formname.require' => '__mail.validata.formname.require__',
            'formname.chsDash' => '__mail.validata.formname.chsDash__',
            'host.require' => '__mail.validata.host.require__',
        ];
        $msg = LangHelper::convertLang($msg,'field');
        $validate = new Validate($rule, $msg);
        $result = $validate->check(input('post.'));
        if(!$result){
            return op_msg(0,$validate->getError());
        }
        return op_msg(1,'success');
    }

    /*
     * 列表
     * */
    public function listData()
    {
        $data  = $this->find();
        if (Request::instance()->isGet()) {
            $log = [
                'op_uid' => Session::get('username'),
                'op_module' => $this->module,
                'op_type' => 'search',
                'op_origin' => $this->table,
                'op_data' => 'find one',
                'op_ip' => getIP(),
                'op_sql' => $this->getLastSql(),
            ];
            LogsM::add($log);
            pr_log($log,'operate');
        }
        return $data;
    }

    /**
     * save & update
     * @return array|mixed
     */
    public function config(){
        $data = input('post.');
        if (isset($data['method']) && $data['method'] == 'test') {
            $test_email = $data['test_email'];
            $subject = $data['formname']." -- TEST-EMAIL";
            $content = "This is TEST EMAIL.<br/> Plz Don't Reply";
            if (email($test_email,$subject,$content,$data)){
                $log = [
                    'op_uid' => Session::get('username'),
                    'op_module' => '邮件测试',
                    'op_type' => 'test_email',
                    'op_origin' => $this->table,
                    'op_data' => json_encode(['to'=>$test_email,'subject'=>$subject,'content'=>$content],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
                    'op_ip' => getIP(),
                    'op_sql' => $this->getLastSql(),
                ];
                LogsM::add($log);
                pr_log($log,'operate');
                return op_success();
            }
            return op_fail();
        }
        $id = $data['id'];
        if (empty($id) && !empty($data)){
            //新增
            $data['updated_at']=time();
            $re = $this->save($data);
            $last_sql = Config::get('app_debug')?$this->getLastSql():'';
            if($re){
                $log = [
                    'op_uid' => Session::get('username'),
                    'op_module' => $this->module,
                    'op_type' => 'add',
                    'op_origin' => $this->table,
                    'op_data' => json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
                    'op_ip' => getIP(),
                    'op_sql' => $this->getLastSql(),
                ];
                LogsM::add($log);
                pr_log($log,'operate');
                return op_success($last_sql);
            }
            return op_fail($last_sql);
        }else {
            //只修改变化字段
            $origin = $this->getById($id)->toArray();
            $data = DBhelper::getChangeData($data,$origin);
            if (!empty($data)){
                $data['updated_at']=time();
                $re = $this->where('id',$id)->update($data);
                $last_sql = Config::get('app_debug')?$this->getLastSql():'';
                if($re){
                    $log = [
                        'op_uid' => Session::get('username'),
                        'op_module' => $this->module,
                        'op_type' => 'edit',
                        'op_origin' => $this->table,
                        'op_data' => json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
                        'op_ip' => getIP(),
                        'op_sql' => $this->getLastSql(),
                    ];
                    LogsM::add($log);
                    pr_log($log,'operate');
                    return op_success($last_sql);
                }
                return op_fail($last_sql);
            }
        }
        return op_nochange();
    }

}